var t = getApp(), a = t.requirejs("core"), e = t.requirejs("biz/order");

Page({
    data: {
        icons: t.requirejs("icons"),
        status: "",
        list: [],
        page: 1,
        code: !1,
        cancel: e.cancelArray,
        cancelindex: 0,
        can_sync_goodscircle: !1,
        totalprice:0,
        ischeckall:false,
        beginTime:'',
        endTime:'',
        isShowTime:false,
    },
    onLoad: function(options) {
        var e = this;
        t.checkAuth(), wx.getSetting({
            success: function(s) {
                s.authSetting["scope.userInfo"] && (e.setData({
                    options: options,
                    status: options.status || "",
                    imgUrl: t.globalData.approot
                }), t.url(options), e.get_list());
            }
        });
       a.get('order/comment/getShowTime',{},function(res){
           e.setData({
            beginTime:res.beginTime,
            endTime:res.endTime,
            isShowTime:e.time_range(res.beginTime,res.endTime)
           });
       })
        
    },
    time_range(beginTime, endTime) {
        var strb = beginTime.split(":");
        // if (strb.length != 2) {
        //   return false;
        // }
        var stre = endTime.split(":");
        // if (stre.length != 2) {
        //   return false;
        // }
        var b = new Date();
        var e = new Date();
        var n = new Date();
        b.setHours(strb[0]);
        b.setMinutes(strb[1]);
        e.setHours(stre[0]);
        e.setMinutes(stre[1]);
        if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
          return true;
        } else {
          return false;
        }
      },
    //是否全选
    ischeckallF(){
        let total=0;
        this.data.list.forEach((item,index)=>{
           if(item.checked){
            total++
           }
        });
        if(total==this.data.list.length){
            this.setData({
                ischeckall:true
            })
        }else{
            this.setData({
                ischeckall:false
            })
        }
    },
    checkall(){
        this.setData({
            ischeckall:!this.data.ischeckall
        });
        if(this.data.ischeckall){
            this.data.list.forEach((item,index)=>{
                let checked='list['+index+'].checked';
                this.setData({
                    [checked]:true
                })
            })
        }else{
            this.data.list.forEach((item,index)=>{
                let checked='list['+index+'].checked';
                this.setData({
                    [checked]:false
                })
            })
        }
        this.setTotalMoney()
    },
    radioC(e){
        let index=e.currentTarget.dataset.index;
        let checked='list['+index+'].checked';
        this.setData({
            [checked]:!this.data.list[index].checked
        });
        this.ischeckallF();
        this.setTotalMoney()
    },
    goDetail(e){
        wx.navigateTo({
          url: '/pages/order/detail/index?id='+e.currentTarget.dataset.id,
        })
    },
    //计算合计价格
    setTotalMoney(){
        let totalprice=0;
        this.data.list.forEach((item,index)=>{
            if(item.checked){
                totalprice+=Number( item.price)
            }
        });
        this.setData({
            totalprice:totalprice
        })
    },
    //待付款一键结算
    goPay(){
        var e = this;
        if(this.data.totalprice==0)return
        let payList =this.data.list.filter(item=>item.checked);
        // console.log("一键结算",payList)
        a.post("order/create/submitSettleOrder", {order:payList}, function(t) {
            e.setData({
                submit: !1
            }), 0 == t.error ? wx.navigateTo({
                url: "/pages/order/pay/index?id=" + t.orderid + "&settle=" + t.settle
            }) : a.alert(t.message);
        }, !0);
    },
    get_list: function() {
        var e = this;
        e.setData({
            loading: !0,
            isgoods: t.globalData.isgoods
        }), a.get("order/get_list", {
            page: e.data.page,
            status: e.data.status,
            merchid: 0
        }, function(t) {
            // console.log(t)
            if(t.list.length>0){
                t.list.forEach(item=>{
                    item.checked=false;
                })
            }
            0 == t.error ? (e.setData({
                loading: !1,
                show: !0,
                total: t.total,
                empty: !0,
                can_sync_goodscircle: t.can_sync_goodscircle
            }), t.list.length > 0 && e.setData({
                page: e.data.page + 1,
                list: e.data.list.concat(t.list)
            }), t.list.length < t.pagesize && e.setData({
                loaded: !0
            })) : a.toast(t.message, "loading");
        }, this.data.show);
    },
    selected: function(t) {
        var e = a.data(t).type;
        this.setData({
            list: [],
            page: 1,
            status: e,
            totalprice:0,
            ischeckall:false,
            empty: !1
        }), this.get_list();
    },
    onReachBottom: function() {
        this.data.loaded || this.data.list.length == this.data.total || this.get_list();
    },
    code: function(t) {
        var e = this, s = a.data(t).orderid;
        a.post("verify/qrcode", {
            id: s
        }, function(t) {
            0 == t.error ? e.setData({
                code: !0,
                qrcode: t.url
            }) : a.alert(t.message);
        }, !0);
    },
    close: function() {
        this.setData({
            code: !1
        });
    },
    cancel: function(t) {
        var s = a.data(t).orderid;
        e.cancel(s, t.detail.value, "/pages/order/index?status=" + this.data.status);
    },
    delete: function(t) {
        var s = a.data(t).type, i = a.data(t).orderid;
        e.delete(i, s, "/pages/order/index", this);
    },
    finish: function(t) {
        a.data(t).type;
        var s = a.data(t).orderid;
        e.finish(s, "/pages/order/index");
    },
    onShareAppMessage: function() {
        return a.onShareAppMessage();
    }
});